<template>
	<view class="page" style="padding-bottom: 120rpx;">
		<topVue title="确认订单"></topVue>
		<view class="productInfo" v-for="(item, index) in productList" :key="index">
			<view class="flex align-center">
				<view class="pictrue">
					<image :src="item.spu.picUrl" style="width: 100%; height: 100%; border-radius: 12rpx;"></image>
				</view>
				<view class="flex-column flex-1 flex-between" style="padding-left: 20rpx; height: 200rpx;">
					<view class="name">{{ item.spu.name }}</view>
					<view class="w100 flex align-center">
						<text style="line-height: 1;" v-for="(ver, i) in item.sku.properties" :key="i">{{ver.valueName}}</text>
					</view>
					<view class="flex-between align-center">
						<view class="flex align-center">
							<image src="../../static/green_g.png" style="width: 40rpx; height: 40rpx;"></image>
							<text class="price">{{ item.sku.price/100 }}</text>
						</view>
						<u-number-box v-model="item.count" @change="valChange(item)" :min="1"></u-number-box>
					</view> 
				</view>
			</view>
		</view>
		
		<!-- 收货地址 -->
		<view class="addressVue margin30 flex align-center"  v-if="defultAddress" @click="$tools.route('../person/addressCenter', 'choose')">
			<image src="../../static/index/map.png" style="width: 38rpx; height: 38rpx;"></image>
			<view class="flex-column flex-1 padding-row30">
				<view class="flex">
					<text style="font-weight: 600; line-height: 1;">{{ defultAddress.name || "" }}</text>
					<text style="font-weight: 600; line-height: 1; padding-left: 20rpx;">{{ defultAddress.mobile || "" }}</text>
				</view>
				<view class="content">{{ defultAddress.areaName || "" }}{{ defultAddress.detailAddress || "" }}</view>
			</view>
			<image src="../../static/more.png" style="width: 40rpx; height: 40rpx;"></image>
		</view>
		<view class="addressVue margin30 flex-between align-center"  v-else @click="$tools.route('../person/addressCenter', 'choose')">
			<image src="../../static/index/map.png" style="width: 38rpx; height: 38rpx;"></image>
			<view class="flex align-center">
				<text style="line-height: 1; color: #454545; font-weight: 600;">选择地址</text>
				<image src="../../static/more.png" style="width: 40rpx; height: 40rpx; margin-left: 20rpx;"></image>
			</view>
		</view>
		
		<!-- 订单备注 -->
		<view class="box padding30">
			<view class="label">订单备注</view>
			<textarea placeholder="请输入备注信息(选填)" v-model="remark"></textarea>
		</view>
		
		<!-- 底部 -->
		<view class="botVue">
			<view class="confirm absolute-auto" @click="toBuy">提交订单</view>
		</view>
		
		<u-popup v-model="show" mode="bottom">
			<view class="popVue padding40">
				<view class="inpVue">
					<view class="ket">支付方式</view>
					<view class="li flex-between align-center padding-row30 margin20">
						<view class="flex align-center">
							<image src="../../static/selected.png" style="width: 50rpx; height: 50rpx;"></image>
							<text style="color: #1CC289; padding-left: 10rpx; line-height: 1; font-size: 30rpx;">绿色积分支付</text>
						</view>
						<text style="line-height: 1; color: #454545; font-size: 30rpx;">可用绿色积分：{{balanceInfo.greenPoints/100}}</text>
					</view>
				</view>
				<view class="inpVue margin30 flex-between">
					<view class="ket">运费</view>
					<text style="color: #454545; line-height: 1; font-size: 30rpx; font-weight: 600;">0</text>
				</view>
				<view class="inpVue margin30 flex-between">
					<view class="ket">抵扣</view>
					<text style="color: #454545; line-height: 1; font-size: 30rpx; font-weight: 600;">0</text>
				</view>
				<view class="inpVue margin30 flex-between">
					<view class="ket">订单总金额</view>
					<view class="flex align-center">
						<image src="../../static/green_g.png" style="width: 40rpx; height: 40rpx;"></image>
						<text style="color: #1CC289; line-height: 1; padding-left: 10rpx; font-size: 30rpx; font-weight: 600;">{{ total/100 }}</text>
					</view>
				</view>
				<view class="submit margin40" @click="show=false; isVerify = true">提交订单</view>
			</view>
		</u-popup>
		
		<u-popup v-model="show20" mode="center" border-radius="12" @close="inviteCode='';">
			<view class="popuVue padding40">
				<view class="title text-center">绑定推广人</view>
				<view class="inpVue margin30 flex-between align-center padding-row20">
					<text style="color: #333; font-weight: 600; line-height: 1;">我的推广码</text>
					<text style="color: #1CC289; font-weight: 600; line-height: 1;">{{ user.id }}</text>
				</view>
				<view class="inpVue margin20 flex align-center">
					<input type="text" v-model="inviteCode" placeholder="请输入推广码(推广人ID)"/>
				</view>
				<view class="btn margin50" @click="bandFun()">确定</view>
			</view>
		</u-popup>
		
		
		<Loading :num="2" v-if="loading"></Loading>
		
		<Verify :show="isVerify" @cancel="isVerify=false" @orderFun="submitConfirm"></Verify>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				quantity: 1,
				productList: [],
				price: 0,
				defultAddress: {},
				remark: "",
				show: false,
				show20: false,
				total: 0,
				items: [],
				payPassword: "",
				inviteCode: "",
				loading: false,
				isVerify: false
			}
		},
		
		async onLoad(option) {
			this.productList = JSON.parse(decodeURI(option.array)) //商品详情
			console.log(this.productList)
			await this.getDefultAddress()
			this.total = 0
			this.items = []
			this.productList.forEach(item=>{
				this.total = this.total + item.count * item.sku.price
				this.items.push({cartId: item.id})
			})
		},
		
		computed: {
			...mapState({
				balanceInfo: state=> state.balanceInfo,
				user: state => state.user
			})
		},
		
		methods: {
			valChange(e){
				console.log(e)
				this.quantity = e.count
				this.$request("/app-api/trade/cart/update-count", "PUT", {
					id: e.id,
					count: e.count
				}).then(res=>{
					if(res.code===0) {
						this.total = 0
						this.items = []
						this.productList.forEach(item=>{
							this.total = this.total + item.count * item.sku.price
							this.items.push({cartId: item.id})
						})
					}	
				})
			},
			
			// 绑定领导人
			async bandFun(){
				if(!this.inviteCode) return this.$u.toast("请输入推广码")
				if(this.user.id==this.inviteCode) return this.$u.toast("不能绑定自己为推广人")
				const res = await this.$request("/app-api/member/user/bindAnInvite", "GET", {
					userId: this.user.id,
					inviter: this.inviteCode
				})
				if(res.code===0) {
					this.$u.toast("绑定成功")
					this.show20 = false
					this.$store.dispatch("getUser", this.user.id)
				}else{
					this.$u.toast(res.msg)
				}
			},
			
			async toBuy(){
				this.$store.dispatch("getBalance", this.user.id)
				const res = await this.$request('/app-api/member/user/check-pay-password', "GET", {})
				if(res.code===0) {
					console.log(res)
					if(res.data) {
						console.log("设置了支付密码")
						this.show = true
					}else {
						this.$u.toast("请设置支付密码")
						setTimeout(()=>{
							uni.navigateTo({
								url: "../person/setup?status=1"
							})
						}, 1000)
					}
				}
			},
			
			// 提交订单
			async submitConfirm(tradePwd){
				this.payPassword = tradePwd
				if(this.loading) return
				if(!this.user.inviter) return this.show20 = true
				if(this.balanceInfo.greenPoints < this.total) return this.$u.toast("余额不足")
				if(!this.defultAddress || this.defultAddress == null) return this.$u.toast("请填写收货地址")
				this.loading = true
				const result = await this.$request("/app-api/trade/order/create", "POST", {
					items: this.items,
					pointStatus: false,
					deliveryType: 1,
					addressId: this.defultAddress.id,
					receiverName: this.defultAddress.name,
					receiverMobile: this.defultAddress.mobile,
					remark: this.remark,
					parendCategoryId: 1,
					parendCategoryName: "订单区"
				})
				if(result.code===0) {
					let payOrderId = result.data.payOrderId
					await this.orderSubmit(payOrderId)
				}else{
					this.loading = false
					this.$u.toast(result.msg)
					console.log(result)
				}
			},
			
			// 提交支付订单
			async orderSubmit(payOrderId){
				//绿色积分支付
				const res = await this.$request("/app-api/pay/order/submit", "POST", {
					id: payOrderId,
					channelCode: "wallet1",
					payPassword: this.payPassword
				})
				console.log(res)
				if(res.code===0 && res.data.status==10) {
					this.loading = false
					this.$u.toast("支付成功")
					this.isVerify = false
					this.$store.dispatch("getBalance", this.user.id)
					setTimeout(()=>{
						this.$tools.back(2)
					},1000)
				}else{
					this.loading = false
					this.$u.toast(res.msg)
				}
			},

			
			// 获取默认地址
			async getDefultAddress(){
				const result = await this.$request("/app-api/member/address/get-default", "GET")
				if(result.code===0) {
					console.log(result)
					this.defultAddress = result.data
					console.log("默认地址", this.defultAddress)
				}
			},
		}
	}
</script>

<style lang="less" scoped>
	.popuVue {
		width: 690rpx;
		.title {
			font-weight: 600;
			font-size: 34rpx;
			line-height: 1;
		}
		.btn {
			width: 100%;
			height: 92rpx;
			background: linear-gradient(to right, #1CC289, #60A5FA);
			border-radius: 15rpx;
			line-height: 92rpx;
			text-align: center;
			color: #fff;
			font-size: 30rpx;
			font-weight: 600;
		}
		.inpVue {
			width: 100%;
			height: 100rpx;
			border-radius: 12rpx;
			border: 1rpx solid #f5f5f5;
			input {
				flex: 1;
				height: 100rpx;
				box-sizing: border-box;
				color: #333;
				padding-left: 20rpx;
				line-height: 1;
				font-size: 28rpx;
			}
			.code {
				color: #1CC289;
				font-weight: 600;
				line-height: 1;
				padding-right: 20rpx;
			}
		}
	}
	.productInfo {
		padding: 30rpx;
		border-top: 1rpx solid #f5f5f5;
		border-bottom: 1rpx solid #f5f5f5;
		.pictrue {
			width: 200rpx;
			height: 200rpx;
		}
		.name {
			color: #333;
			font-weight: 600;
			line-height: 1;
		}
		.price {
			color: #1CC289;
			font-weight: 600;
			font-size: 30rpx;
			line-height: 1;
			padding-left: 10rpx;
		}
	}
	
	.popVue {
		width: 100%;
		.ket {
			line-height: 1;
			color: #333;
			font-weight: 600;
		}
		.li {
			width: 100%;
			height: 100rpx;
			border: 1rpx solid #1CC289;
			border-radius: 14rpx;
		}
		input {
			width: 100%;
			height: 100rpx;
			background: #f5f5f5;
			border-radius: 14rpx;
			box-sizing: border-box;
			padding-left: 20rpx;
			color: #333;
			margin-top: 20rpx;
		}
		.submit {
			width: 690rpx;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 40rpx;
			background: linear-gradient(to right, #1CC289, #60A5FA);
			color: #fff;
			text-align: center;
			font-size: 30rpx;
		}
	}	
	
	.box {
		width: 100%;
		border-bottom: 1rpx solid #f5f5f5;
		.label {
			font-weight: 600;
		}
		textarea {
			width: 100%;
			height: 160rpx;
			border-radius: 12rpx;
			background: #f5f5f5;
			box-sizing: border-box;
			display: block;
			margin-top: 20rpx;
			padding: 20rpx;
			color: #333;
			font-size: 28rpx;
		}
	}
	
	.addressVue {
		width:690rpx;
		padding: 30rpx;
		background: #f5f5f5;
		border-radius: 12rpx;
		.content {
			color: #4B5563;
			line-height: 32rpx;
			padding-top: 20rpx;
		}
	}

	.botVue {
		width: 100%;
		height: 120rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 9;
		padding: 0 30rpx;
		.confirm {
			width: 690rpx;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 40rpx;
			background: linear-gradient(to right, #1CC289, #60A5FA);
			color: #fff;
			text-align: center;
			font-size: 30rpx;
		}
	}
</style>